package com.free.semantic.infrastruct.repository.impl;

import com.free.semantic.infrastruct.common.ModelDO;
import com.free.semantic.infrastruct.mapper.ModelMapper;
import com.free.semantic.infrastruct.repository.ModelRepository;
import com.free.semantic.model.conceptmodel.Model;
import com.free.semantic.model.conceptmodel.ModelCategory;
import com.free.semantic.model.conceptmodel.SemanticNetwork;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 模型Repository实现类
 */
@Repository
public class ModelRepositoryImpl implements ModelRepository {

    @Autowired
    private ModelMapper modelMapper;

    @Override
    public Model findById(Long id) {
        ModelDO modelDO = modelMapper.findById(id);
        if (modelDO == null) {
            return null;
        }
        return convertToModel(modelDO);
    }

    @Override
    public List<Model> findAll() {
        List<ModelDO> modelDOs = modelMapper.findAll();
        return modelDOs.stream()
                .map(this::convertToModel)
                .collect(Collectors.toList());
    }

    @Override
    public List<Model> findByCategory(ModelCategory category) {
        List<ModelDO> modelDOs = modelMapper.findByCategory(category.name());
        return modelDOs.stream()
                .map(this::convertToModel)
                .collect(Collectors.toList());
    }

    @Override
    public List<Model> findByPublisher(String publisher) {
        List<ModelDO> modelDOs = modelMapper.findByPublisher(publisher);
        return modelDOs.stream()
                .map(this::convertToModel)
                .collect(Collectors.toList());
    }

    @Override
    public Model findBySemanticNetworkId(Long semanticNetworkId) {
        ModelDO modelDO = modelMapper.findBySemanticNetworkId(semanticNetworkId);
        if (modelDO == null) {
            return null;
        }
        return convertToModel(modelDO);
    }

    @Override
    public Long save(Model model) {
        ModelDO modelDO = convertToDO(model);
        modelMapper.insert(modelDO);
        return modelDO.getId();
    }

    @Override
    public void update(Model model) {
        ModelDO modelDO = convertToDO(model);
        modelMapper.update(modelDO);
    }

    @Override
    public void incrementUsageCount(Long id) {
        modelMapper.incrementUsageCount(id);
    }

    @Override
    public void deleteById(Long id) {
        modelMapper.deleteById(id);
    }

    /**
     * 将DO转换为Model
     */
    private Model convertToModel(ModelDO modelDO) {
        if (modelDO == null) {
            return null;
        }
        
        Model model = new Model();
        model.setId(modelDO.getId());
        model.setName(modelDO.getName());
        model.setDescription(modelDO.getDescription());
        model.setPublisher(modelDO.getPublisher());
        model.setUsageCount(modelDO.getUsageCount());
        model.setSemanticNetworkId(modelDO.getSemanticNetworkId());
        
        // 转换枚举
        try {
            model.setCategory(ModelCategory.valueOf(modelDO.getCategory()));
        } catch (Exception e) {
            model.setCategory(ModelCategory.OTHER);
        }
        
        return model;
    }

    /**
     * 将Model转换为DO
     */
    private ModelDO convertToDO(Model model) {
        if (model == null) {
            return null;
        }
        
        ModelDO modelDO = new ModelDO();
        modelDO.setId(model.getId());
        modelDO.setName(model.getName());
        modelDO.setDescription(model.getDescription());
        modelDO.setPublisher(model.getPublisher());
        modelDO.setUsageCount(model.getUsageCount());
        modelDO.setSemanticNetworkId(model.getSemanticNetworkId());
        
        // 转换枚举为字符串
        if (model.getCategory() != null) {
            modelDO.setCategory(model.getCategory().name());
        } else {
            modelDO.setCategory(ModelCategory.OTHER.name());
        }
        
        return modelDO;
    }
} 